<template>
  <div class="vehicle-list-page">
    <div class="vehicle-container">
      <div class="vehicle-header">
        <button @click="$emit('go-back')" class="back-button">← 返回账号列表</button>
        <h2>车辆列表 ({{ vehicles.length }} 辆车)</h2>
      </div>
      
      <div v-if="loadingVehicles" class="loading">
        <p>正在加载车辆数据...</p>
      </div>
      
      <div v-else-if="vehicles.length === 0" class="empty">
        <p>暂无车辆数据</p>
      </div>
      
      <div v-else class="vehicle-cards">
        <div 
          v-for="(vehicle, index) in vehicles" 
          :key="index"
          class="vehicle-card"
          @click="$emit('vehicle-click', vehicle)"
        >
          <div class="card-header">
            <span class="plate-number">{{ vehicle.plateNumber || '未上牌' }}</span>
          </div>
          <div class="card-body">
            <div class="info-row">
              <span class="label">车辆ID:</span>
              <span class="value">{{ vehicle.clientVehicleId || '-' }}</span>
            </div>
            <div class="info-row">
              <span class="label">客户姓名:</span>
              <span class="value">{{ vehicle.name || '-' }}</span>
            </div>
            <div class="info-row">
              <span class="label">手机号码:</span>
              <span class="value">{{ vehicle.phone || '-' }}</span>
            </div>
            <div class="info-row">
              <span class="label">上次进厂:</span>
              <span class="value">{{ formatDate(vehicle.serviceRecord?.startTime) }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue'

const props = defineProps({
  vehicles: {
    type: Array,
    required: true,
    default: () => []
  },
  loadingVehicles: {
    type: Boolean,
    required: true,
    default: false
  }
})

defineEmits(['go-back', 'vehicle-click'])

function formatDate(timestamp) {
  if (!timestamp) return '-'
  const date = new Date(timestamp)
  return date.toLocaleDateString('zh-CN')
}
</script>

<style scoped>
.vehicle-list-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.vehicle-container {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 1.5rem;
}

.vehicle-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 2px solid #e5e7eb;
}

.vehicle-header h2 {
  color: #42b983;
  margin: 0;
}

.back-button {
  background-color: #6c757d;
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: background-color 0.2s;
}

.back-button:hover {
  background-color: #5a6268;
}

.loading, .empty {
  text-align: center;
  padding: 2rem;
  color: #64748b;
}

.vehicle-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1rem;
}

.vehicle-card {
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 1rem;
  transition: transform 0.2s, box-shadow 0.2s;
  cursor: pointer;
}

.vehicle-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
  margin-bottom: 0.75rem;
  padding-bottom: 0.75rem;
  border-bottom: 2px solid #42b983;
}

.plate-number {
  font-size: 1.25rem;
  font-weight: bold;
  color: #42b983;
  letter-spacing: 0.1em;
}

.card-body {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.info-row {
  display: flex;
  justify-content: space-between;
  padding: 0.25rem 0;
}

.label {
  color: #64748b;
  font-size: 0.9rem;
}

.value {
  color: #1e293b;
  font-weight: 500;
}
</style>

